//
//  TuJiaAutoLayoutPopupViewLayouts.swift
//  PMSBase
//
//  Created by WangWenhui on 2017/8/15.
//  Copyright © 2017年 TuJia. All rights reserved.
//

import Foundation

public class TuJiaAutoLayoutPopupViewLayouts: NSObject {
    
    fileprivate static let center = CenterPopupLayoutDelegate()
    fileprivate static let page = FloatPageLayoutDelegate()
    fileprivate static let centerAutoWidth = CenterAutoWidthPopupLayoutDelegate()
    
    public enum EnumLayoutStyle {
        case center
        case page
        case centerAutoWidth
    }
    
    static public func getLayoutDelegate(_ style: EnumLayoutStyle) -> TuJiaAutoLayoutPopupViewLayoutDelegate {
        switch style {
        case .center:
            return center
        case .page:
            return page
        case .centerAutoWidth:
            return centerAutoWidth
        }
    }
}

// MARK: - 因为房态功能的需要，开始剥离TuJiaAutoLayoutPopupView的动画布局
public protocol TuJiaAutoLayoutPopupViewLayoutDelegate: class {
    
    /// 给弹层最外层的内容容器进行设置（如样式、约束）。该方法仅在视图初始化时执行一次
    func settingForPopupContentView(_ content: UIView, lastContentView last: UIView)
    
    func settingForPopupAnimationDynamicConstaints(_ content: UIView, isHide: Bool)
}

@objc public protocol TuJiaAutoLayoutPopupViewEventDelegate: class {
    
    @objc optional func onPopupViewShowAnimationCompletion(_ sender: TuJiaAutoLayoutPopupView)
    
    @objc optional func onPopupViewHideAnimationCompletion(_ sender: TuJiaAutoLayoutPopupView)
}


public class CenterPopupLayoutDelegate: NSObject, TuJiaAutoLayoutPopupViewLayoutDelegate {
    
    public func settingForPopupContentView(_ content: UIView, lastContentView last: UIView) {
        
        content.setCorner(10)
        content.snp.makeConstraints { (make) in
            make.width.equalTo(TuJiaAutoLayoutPopupView.contentFixedWidth)
            make.bottom.equalTo(last)
        }
    }
    
    public func settingForPopupAnimationDynamicConstaints(_ content: UIView, isHide: Bool) {
        
        if let popView = content.superview {
            content.snp_makeConstraintsDynamic { (make) in
                if isHide {
                    // 隐藏时设置视图在屏幕外
                    make.centerX.equalTo(popView)
                    make.centerY.equalTo(popView).offset(CommonDefine.kScreenWidth)
                }
                else {
                    // 非隐藏时设置在屏幕中
                    make.center.equalTo(popView)
                }
            }
        }
    }
}

public class CenterAutoWidthPopupLayoutDelegate: CenterPopupLayoutDelegate {
    
    override public func settingForPopupContentView(_ content: UIView, lastContentView last: UIView) {
        
        content.setCorner(VS.corner3)
        content.snp.makeConstraints { (make) in
            make.bottom.equalTo(last)
        }
    }
}

public class FloatPageLayoutDelegate: NSObject, TuJiaAutoLayoutPopupViewLayoutDelegate {
    
    public func settingForPopupContentView(_ content: UIView, lastContentView last: UIView) {
        
        content.backgroundColor = UIColor.white
        content.snp.makeConstraints { (make) in
            if let superview = content.superview {
                make.centerX.equalTo(superview)
            }
            make.width.equalTo(CommonDefine.kScreenWidth)
            make.height.equalTo(CommonDefine.kScreenFullHeight * 3.0/4)
        }
    }
    
    public func settingForPopupAnimationDynamicConstaints(_ content: UIView, isHide: Bool) {
        
        if let popView = content.superview {
            content.snp_makeConstraintsDynamic { (make) in
                if isHide {
                    // 隐藏时最顶部在屏幕外
                    make.top.equalTo(popView.snp.bottom)
                } else {
                    // 非隐藏时最底部在屏幕内
                    make.bottom.equalTo(popView)
                }
            }
        }
    }
}
